package com.itheima.aop;

import com.itheima.mapper.OperateLogMapper;
import com.itheima.pojo.OperateLog;
import com.itheima.utils.CurrentHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//@Aspect这个是定义这个类是切面类
@Aspect
@Component
public class OperationAspect {
    @Autowired
    private OperateLogMapper operateLogMapper;

    //@Around这个方法是环绕逻辑
    @Around("@annotation(com.itheima.annotation.AopAnnotation)")
    public Object arount(ProceedingJoinPoint pjp) throws Throwable {
        OperateLog operateLog =new OperateLog();

        operateLog.setOperateEmpId(CurrentHolder.get());
        operateLog.setOperateTime(LocalDateTime.now());
        operateLog.setClassName(pjp.getTarget().getClass().getName());
        operateLog.setMethodName(pjp.getSignature().getName());
        long start = System.currentTimeMillis();
        Object result = pjp.proceed();
        operateLog.setMethodParams(pjp.getArgs().toString());
        operateLog.setReturnValue(result.toString());
        long end = System.currentTimeMillis();
        operateLog.setCostTime(end-start);
        operateLogMapper.add(operateLog);
        return result;
    }


}
